package com.intelligent.utils;

import com.intelligent.entity.OperationLogs;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

public class OperationLogUtil {

    public static OperationLogs buildLog(String employeeAccount, Integer userId,
            String module, String content) {
        OperationLogs log = new OperationLogs();
        log.setLogNo("RZ" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 6));
        log.setEmployeeAccount(employeeAccount);
        log.setUId(userId != null ? Long.valueOf(userId) : null);
        log.setOperationModule(module);
        log.setOperationContent(content);
        log.setOperationTime(new Date());
        // 从邮箱账号中提取用户名作为操作员
        String operator = extractUsernameFromEmail(employeeAccount);
        log.setOperator(operator);
        return log;
    }

    /**
     * 从邮箱地址中提取用户名
     */
    private static String extractUsernameFromEmail(String email) {
        if (email != null && email.contains("@")) {
            return email.substring(0, email.indexOf("@"));
        }
        return null; // 不使用默认操作员
    }
}
